﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cherry.Agile.Core;
using Microsoft.Practices.Unity;
using System.IO;
namespace Cherry.Agile.FileHandling
{
    /// <summary>
    /// 文件存储管理
    /// </summary>
    public static class FileManager
    {
        static IFileHandler handler;

        static FileManager()
        {
            if (Container.Current.IsRegistered<IFileHandler>())
                handler = Container.Current.Resolve<IFileHandler>();
            else
                handler = new FileHandler();
        }

        /// <summary>
        /// 获取一个<see cref="IFileHandler"/>
        /// </summary>
        /// <param name="name">IFileHandler对应的配置名称</param>
        /// <returns></returns>
        public static IFileHandler Create(string name)
        {
            return Container.Current.Resolve<IFileHandler>(name);
        }

        /// <summary>
        /// 将一个文件存储到系统中
        /// </summary>
        /// <param name="file">文件路径</param>
        /// <returns>存储完毕的实际文件名称，文件名称会重新生成</returns>
        public static string SaveFile(string file)
        {
            return SaveFile(file, true);
        }

        /// <summary>
        /// 将一个文件存储到系统中
        /// </summary>
        /// <param name="file">文件路径</param>
        /// <param name="folder">存储文件夹</param>
        /// <returns>存储完毕的实际文件名称，文件名称会重新生成</returns>
        public static string SaveFile(string file,string folder)
        {
            return SaveFile(file,folder, true);
        }

        /// <summary>
        /// 将一个文件存储到系统中
        /// </summary>
        /// <param name="file">文件路径</param>
        /// <param name="rename">是否重新命名文件</param>
        /// <returns>存储完毕的实际文件名称</returns>
        public static string SaveFile(string file, bool rename)
        {
            return handler.SaveFile(file,string.Empty, rename);
        }
        
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="file"></param>
        /// <param name="folder"></param>
        /// <param name="rename"></param>
        /// <returns></returns>
        public static string SaveFile(string file,string folder, bool rename)
        {
            return handler.SaveFile(file, folder, rename);
        }

        /// <summary>
        /// 将一个文件流存储到系统中
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <param name="inputStream">文件流</param>
        /// <returns>存储后的文件名</returns>
        public static string SaveFile(string fileName, System.IO.Stream inputStream)
        {
            return SaveFile(fileName,string.Empty,inputStream, false);
        }
        /// <summary>
        /// 将一个文件流存储到系统中
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <param name="inputStream">文件流</param>
        /// <param name="rename">是否重新命名</param>
        /// <returns></returns>
       
        public static string SaveFile(string fileName, System.IO.Stream inputStream, bool rename)
        {
            return handler.SaveFile(fileName, string.Empty,inputStream, rename);
        }
        /// <summary>
        /// 将一个文件流存储到系统中
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <param name="inputStream">文件流</param>
        /// <param name="rename">是否重新命名</param>
        /// <param name="folder">存储文件夹</param>
        /// <returns></returns>
        public static string SaveFile(string fileName, string folder, System.IO.Stream inputStream, bool rename)
        {
            return handler.SaveFile(fileName, folder,inputStream, rename);
        }

        /// <summary>
        /// 打开一个文件流
        /// </summary>
        /// <param name="name">文件名称</param>
        /// <returns>文件流</returns>
        public static System.IO.Stream OpenFile(string name)
        {
            return handler.OpenFile(name);
        }

        /// <summary>
        /// 打开一个文件流
        /// </summary>
        /// <param name="name">文件名称</param>
        /// <param name="mode"><see cref="FileMode"/></param>
        /// <returns>文件流</returns>
        public static System.IO.Stream OpenFile(string name, FileMode mode)
        {
            return handler.OpenFile(name,mode);
        }


        /// <summary>
        /// 获取文件信息
        /// </summary>
        /// <param name="name">文件名称</param>
        /// <returns>文件信息</returns>
        public static FileInfomation GetFileInformation(string name)
        {
            return handler.GetFileInformation(name);
        }
        
        /// <summary>
        /// 移除文件
        /// </summary>
        /// <param name="name">文件的全名称</param>
        /// <returns></returns>
        public static bool Remove(string name)
        {
            return handler.Remove(name);
        }

        /// <summary>
        /// 扩展数据流添加字节数据的方法
        /// </summary>
        /// <param name="stream">stream</param>
        /// <param name="buffer">buffer</param>
        public static void Append(this Stream stream, byte[] buffer)
        {
            stream.Seek(0, SeekOrigin.End);
            stream.Write(buffer, 0, buffer.Length);
        }

        /// <summary>
        /// 扩展数据流添加写入文件的方法
        /// </summary>
        /// <param name="stream">stream</param>
        /// <param name="text">text string</param>
        public static void Append(this Stream stream, string text)
        {
            byte[] buffer=System.Text.Encoding.UTF8.GetBytes(text);
            stream.Append(buffer);
        }

         /// <summary>
            /// 给文件重新命名
            /// </summary>
            /// <param name="oldFileName">旧的文件名</param>
            /// <param name="newFileName">新的文件名</param>
            /// <returns>成功返回true,否则返回false</returns>
        public static bool Rename(string oldFileName, string newFileName)
        {
            return handler.Rename(oldFileName, newFileName);
        }

 
    }
}
